/*
 * @Author: thinker_cy
 * @Date: 2025-07-02 17:40:18
 * @FilePath: \weimai-admin\src\config\axios.js
 * @Description: 
 */
import axios from "axios";
import { ElMessage } from 'element-plus'
import qs from 'qs'


const service = axios.create({
    baseURL: import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL,
    timeout: import.meta.env.VITE_REQUEST_TIMEOUT
})

// 请求拦截器
service.interceptors.request.use((config) => {
    const params = config.params || {}
  // get参数编码
  if (config.method?.toUpperCase() === 'GET' && params) {
    config.params = {}
    const paramsStr = qs.stringify(params, { allowDots: true })
    if (paramsStr) {
      config.url = config.url + '?' + paramsStr
    }
  }
  return config
})

// 响应拦截器
service.interceptors.response.use((response) => {
  const res = response.data
  if (res.code !== 0) {
    ElMessage({
      message: res.msg,
      type: 'warning',
    })
    return Promise.reject(new Error(res.message || 'Error'))
  } else {
    return res.data
  }
})

export default service